package com.ojoin.trade.common_core.utils;

import javax.servlet.http.HttpServletRequest;

import com.ojoin.trade.common.exception.BusinessException;
import org.apache.shiro.session.Session;

import com.ojoin.trade.common.Constants;
import com.ojoin.trade.common.constdef.IConstDef;
import com.ojoin.trade.common.exception.ControllerException;
import com.ojoin.trade.common.utils.CommonUtils;
import com.ojoin.trade.common.web.domain.SessionUser;
import com.ojoin.trade.common.web.shiro.Principal;
import com.ojoin.trade.common_core.web.domain.MerchantUserInfo;

public class TradeUtils {

	/**
	 * 获取登录九盈用户id
	 */
	public static int getJoinId(){
		Principal principal = CommonUtils.getPrincipal();
		if (principal!=null){
			if(IConstDef.loginType_Factor.equals(principal.getLoginType())){
				return CommonUtils.getSysUser().getCompany().getId();
			}else{
				return Integer.parseInt(principal.getId());
			}
		}else{
			throw new ControllerException("长时间未操作，请重新登录");
		}
	}

	/**
	 * 获取登录保理员对应的保理公司所属九盈用户id
	 */
	public static int getOperatorCompany(HttpServletRequest request){
		Integer id = CommonUtils.getSysUser().getCompany().getId();
		//TODO:using role
		if(1==id.intValue()){
			throw new BusinessException("不允许管理员操作业务！");
		}
		return id;
	}

	/**
	 * 获取登录保理员对应的保理公司所属九盈用户MhtNo
	 */
	public static String getOperatorCompanyMhtNo(){
		return CommonUtils.getSysUser().getCompany().getMaster();
	}

	/**
	 * 获取登录商户信息
	 */
	public static MerchantUserInfo getMerchUser(HttpServletRequest request){
		Session session=CommonUtils.getSession();
		if(null==session){
			throw new ControllerException("长时间未操作，请重新登录");
		}
		MerchantUserInfo data=(MerchantUserInfo)session.getAttribute(Constants.SESSION_MERCHANTUSER);
		return data;
	}
	
	
	/**
	 * 获取登录商户ID
	 */
	public static String getMhtNo(HttpServletRequest request){
		SessionUser data=CommonUtils.getSessionUser(request);
		return data.getMhtNo();
	}
    public static String getMchName(HttpServletRequest request){
        SessionUser data=CommonUtils.getSessionUser(request);
        return data.getMchName();
    }



}
